package com.songcha.library_common.ui.view

import android.content.Context
import android.graphics.BlurMaskFilter
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.StateListDrawable
import android.os.Build
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import androidx.core.view.marginBottom
import androidx.core.view.marginLeft
import androidx.core.view.marginRight
import androidx.core.view.marginTop
import com.songcha.library_common.R
import com.songcha.library_common.util.AlgorithmUtil
import com.songcha.library_common.util.ColorUtil
import com.songcha.library_common.util.dp2px


/***
 * 自定义容器，可添加圆角，边框，阴影
 * 阴影颜色必须加透明度，不能为全透明或全不透明
 */
@Deprecated("not use")
class CustomContainerLayout_
@JvmOverloads
constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defRes: Int = 0) : ViewGroup(context,attrs,defStyle,defRes) {
    companion object{
        private val DEFAULTT_SHADOW_BLUR_RADIUS=5f.dp2px()
    }


    private var mRadius=FloatArray(4)
    private var mHasRadius=false
    private var mBackgroundColor=Color.TRANSPARENT
    private var mPressColor=Color.TRANSPARENT
    private var mDisableColor=Color.GRAY
    private var mBorderPaint:Paint?=null//边框画笔
    private var mShapePaint:Paint?=null//背景画笔
    private var mShadowPaint:Paint?=null
    private var mShadowRectRadius=0f//阴影圆角半径
    //var mShadowWidth=0f//阴影宽度
    private var mBorderWidth=0f//边框宽度
    private var mBorderColor=Color.TRANSPARENT
    private var mStartColor=Color.TRANSPARENT//渐变起始色
    private var mMiddleColor=Color.TRANSPARENT//渐变中间色
    private var mEndColor=Color.TRANSPARENT//渐变结束色
    private var mShadowColor=Color.TRANSPARENT

    private var mShowShadow=false
    private var mShadowRectF=RectF()
    //阴影宽度
    private var mShadowLeftWidth=0f
    private var mShadowTopWidth=0f
    private var mShadowRightWidth=0f
    private var mShadowBottomWidth=0f

    //阴影偏移
    private var mShadowLeftOffset=0f
    private var mShadowTopOffset=0f
    private var mShadowRightOffset=0f
    private var mShadowBottomOffset=0f
    private var mShadowBlurRadius=0f

    private var mBorderRectF=RectF()
    private var mShapeRectF=RectF()
    private var mClipPath=Path()
    private var mShadowClipPath=Path()
    private var mBorderPath=Path()
    private var mShapePath=Path()
    private var mLinearGradient:LinearGradient?=null
    private var mShadowDirection=0x2 or 0x4 or 0x8 or 0x10
    private var mGradientModel=1
    //是否只绘制底部边框
    private var mOnlyBottomBorder=false
    private var mIsChildCenter=false
    private var mPath=Path()


    private var mRadii=FloatArray(8)



    init {
        initView(context,attrs)
    }



    private fun initView(context: Context, attrs: AttributeSet?){

        setWillNotDraw(false)

      /*  if(background is ColorDrawable){
            mBackgroundColor=(background as ColorDrawable).color
        }else{
            mBackgroundColor=Color.WHITE
        }

        */

        //必须设置透明，否则裁剪后还会有背景
        setBackgroundColor(Color.TRANSPARENT)


        if(attrs==null) return
        val ta=context.obtainStyledAttributes(attrs, R.styleable.CustomContainerLayout_)
        val l=ta.getDimension(R.styleable.CustomContainerLayout__left_radius,0f)
        val t=ta.getDimension(R.styleable.CustomContainerLayout__top_radius,0f)
        val r=ta.getDimension(R.styleable.CustomContainerLayout__right_radius,0f)
        val b=ta.getDimension(R.styleable.CustomContainerLayout__bottom_radius,0f)
        val radius=ta.getDimension(R.styleable.CustomContainerLayout__radius,0f)
        mBackgroundColor=ta.getColor(R.styleable.CustomContainerLayout__background_color_,Color.TRANSPARENT)

        val hasRipple=ta.getBoolean(R.styleable.CustomContainerLayout__has_ripple,false)

        //添加点击波纹效果,6.0以上有效，edittext不设置，因为获取焦点时也会显示
        //if(childCount>0 && getChildAt(0) !is EditText){
        if(hasRipple){
            val ta2 = context.obtainStyledAttributes(attrs, androidx.appcompat.R.styleable.AppCompatTheme)
            val f=ta2.getDrawable(androidx.appcompat.R.styleable.AppCompatTheme_selectableItemBackground)
            if(Build.VERSION.SDK_INT>=23){
                foreground=f
            }
            ta2.recycle()
        }

        //}

        if(radius>0){
            mRadius[0]=radius
            mRadius[1]=radius
            mRadius[2]=radius
            mRadius[3]=radius
        }else{
            mRadius[0]=l
            mRadius[1]=t
            mRadius[2]=r
            mRadius[3]=b
        }


        mShadowColor=ta.getColor(R.styleable.CustomContainerLayout__shadow_color,Color.TRANSPARENT)
        mShadowBlurRadius=ta.getDimension(R.styleable.CustomContainerLayout__shadow_blur_radius,0f)
        //mShadowWidth=ta.getDimension(R.styleable.CustomContainerLayout_shadow_width,0f)
        //mShadowWidth=shadow_blur_radius
        mShadowRectRadius=ta.getDimension(R.styleable.CustomContainerLayout__shadow_rect_radius,0f)
        mShowShadow=ta.getBoolean(R.styleable.CustomContainerLayout__show_shadow,false)
        mShadowLeftOffset=ta.getDimension(R.styleable.CustomContainerLayout__shadow_left_offset,0f)
        mShadowTopOffset=ta.getDimension(R.styleable.CustomContainerLayout__shadow_top_offset,0f)
        mShadowRightOffset=ta.getDimension(R.styleable.CustomContainerLayout__shadow_right_offset,0f)
        mShadowBottomOffset=ta.getDimension(R.styleable.CustomContainerLayout__shadow_bottom_offset,0f)

        mShadowDirection=ta.getInt(R.styleable.CustomContainerLayout__shadow_direction,0x2 or 0x4 or 0x8 or 0x10)
        mBorderWidth=ta.getDimension(R.styleable.CustomContainerLayout__border_width,0f)
        mBorderColor=ta.getColor(R.styleable.CustomContainerLayout__border_color,Color.TRANSPARENT)
        //mClickColor=ta.getColor(R.styleable.CustomContainerLayout_click_color,Color.TRANSPARENT)
        mStartColor=ta.getColor(R.styleable.CustomContainerLayout__start_color,Color.TRANSPARENT)
        mMiddleColor=ta.getColor(R.styleable.CustomContainerLayout__middle_color,Color.TRANSPARENT)
        mEndColor=ta.getColor(R.styleable.CustomContainerLayout__end_color,Color.TRANSPARENT)
        mGradientModel=ta.getInt(R.styleable.CustomContainerLayout__gradient_model,1)

        mIsChildCenter=ta.getBoolean(R.styleable.CustomContainerLayout__is_child_center,false)

        mOnlyBottomBorder=ta.getBoolean(R.styleable.CustomContainerLayout__only_bottom_border,false)

        if(mShowShadow){
            mBackgroundColor=Color.WHITE
        }


        //if(mClickColor!=Color.TRANSPARENT){
            //isClickable=true
       // }

        ta.recycle()

        //setBorderPaint()

       /* if(mBackgroundColor!=Color.TRANSPARENT){
            mShapePaint=Paint()
            mShapePaint!!.color=mBackgroundColor
            mShapePaint!!.isAntiAlias=true
            mShapePaint!!.style=Paint.Style.FILL
        }*/

        initShadow()

        //this.setPadding(pl,pt,pr,pb)

    }

    private fun initShadow(){
        if(!mShowShadow && (mShadowColor!=Color.TRANSPARENT || mShadowBlurRadius>0 || mShadowRectRadius>0) || mShadowDirection!=0x2 or 0x4 or 0x8 or 0x10)
            mShowShadow=true

        if(mShowShadow){
            //禁用硬件加速
            setLayerType(LAYER_TYPE_SOFTWARE, null)

            if(mShadowColor==Color.TRANSPARENT) mShadowColor= ColorUtil.getResourceColor(context,R.color.shadow)
            if(mShadowBlurRadius==0f) mShadowBlurRadius=DEFAULTT_SHADOW_BLUR_RADIUS
            // if(mShadowWidth==0f) mShadowWidth=4f.dp2px()
            if(mShadowRectRadius==0f){
                //设置为最大的radius
                var maxRadius=0f
                for(i in mRadius.indices){
                    if(mRadius[i]>maxRadius)
                        maxRadius=mRadius[i]
                }
                mShadowRectRadius=maxRadius
            }


        }

        //padding=mShadowBlurRadius
        //val padding=(mShadowBlurRadius).toInt()

        if(mShowShadow){
            if(mShadowDirection and 0x2==0x2){
                mShadowLeftWidth = mShadowBlurRadius
            }
            if(mShadowDirection and 0x4==0x4){
                mShadowTopWidth = mShadowBlurRadius
            }
            if(mShadowDirection and 0x8==0x8){
                mShadowRightWidth = mShadowBlurRadius
            }
            if(mShadowDirection and 0x10==0x10){
                mShadowBottomWidth = mShadowBlurRadius
            }
        }
    }

    fun setShowShadow(showShadow:Boolean){
        mShowShadow=showShadow
        initShadow()
        initDrawable()
    }

    fun setShadowBlur(blur:Float){
        mShadowBlurRadius=blur
        initShadow()
        initDrawable()
    }

    fun setShadowColor(color:Int){
        mShadowColor=color
        initShadow()
        initDrawable()
    }

    private fun setBorderPaint(){
        if(mBorderWidth>0 && mBorderColor!=Color.TRANSPARENT){
            mBorderPaint=Paint()
            mBorderPaint!!.color=mBorderColor
            mBorderPaint!!.isAntiAlias=true
            mBorderPaint!!.style=Paint.Style.STROKE
            mBorderPaint!!.strokeWidth=mBorderWidth
        }else{
            mBorderPaint=null
        }
    }

    fun setBorder(borderColor:Int,borderWidth:Float){
        mBorderColor=borderColor
        mBorderWidth=borderWidth
        initDrawable()
        //setBorderPaint()
        //invalidate()
    }

    fun setBorderWidth(borderWidth:Float){
        mBorderWidth=borderWidth
        initDrawable()
        //setBorderPaint()
        //invalidate()
    }

    fun setBorderColor(borderColor:Int){
        mBorderColor=borderColor
        initDrawable()
       // setBorderPaint()
        //invalidate()
    }

    /*private fun setShapePaint(){
        if(mBackgroundColor!=Color.TRANSPARENT){
            mShapePaint=Paint()
            mShapePaint!!.color=mBackgroundColor
            mShapePaint!!.isAntiAlias=true
            mShapePaint!!.style=Paint.Style.FILL
        }
    }*/

    private fun setShadowPaint(){
        if(mShowShadow){
            mShadowPaint=Paint()
            //mShadowPaint!!.color=Color.TRANSPARENT
            mShadowPaint!!.color=mShadowColor
            mShadowPaint!!.isAntiAlias=true

           // mShadowPaint!!.setShadowLayer(mShadowBlurRadius,0f,0f,mShadowColor)
            mShadowPaint!!.maskFilter=BlurMaskFilter(mShadowBlurRadius,BlurMaskFilter.Blur.NORMAL)
        }

    }




    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        //if(visibility!=View.VISIBLE) return
        if(childCount==0){
            val v=View(context)
            val lp=LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)
            v.layoutParams=lp
            addView(v)
        }


        val widthMode=MeasureSpec.getMode(widthMeasureSpec)
        val widthSize=MeasureSpec.getSize(widthMeasureSpec)
        val heightMode=MeasureSpec.getMode(heightMeasureSpec)
        val heightSize=MeasureSpec.getSize(heightMeasureSpec)

        //if(tag!=null)
           // LogUtil.log("aaa",tag,measuredWidth,widthSize,width)

        //exactly时不能修改尺寸

        //val sw=(widthSize+mShadowLeftWidth+mShadowRightWidth).toInt()
        //val sh=(heightSize+mShadowTopWidth+mShadowBottomWidth).toInt()

        //child加了margin尺寸
        var cmw=0
        var cmh=0
        //padding尺寸
        var pw=paddingLeft+paddingRight
        var ph=paddingTop+paddingBottom

        if(tag=="test"){
            val a=0
        }

        if(childCount>0){
            //因为给child设置了background，所以如果是exactly要设置child的尺寸
            val child=getChildAt(0)
            //child.setBackgroundColor(Color.TRANSPARENT)

            val childLp= child.layoutParams as MarginLayoutParams

            val childWidthMeasureSpec=getChildMeasureSpec(widthMeasureSpec,0,childLp.width)
            val childHeightMeasureSpec=getChildMeasureSpec(heightMeasureSpec,0,childLp.height)
            val childWidthMode=MeasureSpec.getMode(childWidthMeasureSpec)
            val childHeightMode=MeasureSpec.getMode(childHeightMeasureSpec)

            //at_most时为最大尺寸，要设置成最小尺寸
            if(widthMode==MeasureSpec.EXACTLY && heightMode==MeasureSpec.EXACTLY){
                //测量child需要减去阴影尺寸
                val newWidthMeasureSpec=MeasureSpec.makeMeasureSpec((widthSize-mShadowLeftWidth-mShadowRightWidth).toInt(),MeasureSpec.EXACTLY)
                val newHeightMeasureSpec=MeasureSpec.makeMeasureSpec((heightSize-mShadowTopWidth-mShadowBottomWidth).toInt(),MeasureSpec.EXACTLY)
                //无阴影时不需要铺满
                if(isChildFillParent()){
                    child.measure(newWidthMeasureSpec,newHeightMeasureSpec)
                }else{
                    measureChildWithMargins(child,widthMeasureSpec,0,heightMeasureSpec,0)
                }
                //测量child时减去了阴影尺寸，所以要加回来
                cmw=(child.measuredWidth+child.marginLeft+child.marginRight+mShadowLeftWidth+mShadowRightWidth).toInt()
                cmh=(child.measuredHeight+child.marginTop+child.marginBottom+mShadowTopWidth+mShadowBottomWidth).toInt()
                setMeasuredDimension(widthSize,heightSize)
            }else if(widthMode==MeasureSpec.EXACTLY){
                //测量child需要减去阴影尺寸
                var newHeightMeasureSpec:Int
                val newWidthMeasureSpec=MeasureSpec.makeMeasureSpec((widthSize-mShadowLeftWidth-mShadowRightWidth).toInt(),MeasureSpec.EXACTLY)
                if(childHeightMode==MeasureSpec.EXACTLY){
                    //此处应为child width
                    newHeightMeasureSpec=MeasureSpec.makeMeasureSpec(childLp.height,MeasureSpec.EXACTLY)
                }else{
                    //TODO 不安全，不能修改child尺寸，因为会多次调用
                    newHeightMeasureSpec=MeasureSpec.makeMeasureSpec((heightSize-mShadowTopWidth-mShadowBottomWidth+childLp.topMargin+childLp.bottomMargin).toInt(),MeasureSpec.AT_MOST)
                }
                //无阴影或边框时不需要铺满
                if(isChildFillParent()){
                    child.measure(newWidthMeasureSpec,newHeightMeasureSpec)
                }else{
                    measureChildWithMargins(child,widthMeasureSpec,0,heightMeasureSpec,0)
                }

              /*  val newWidthMeasureSpec=MeasureSpec.makeMeasureSpec((widthSize-mShadowLeftWidth-mShadowRightWidth).toInt(),MeasureSpec.EXACTLY)
                val newHeightMeasureSpec=MeasureSpec.makeMeasureSpec((heightSize-mShadowTopWidth-mShadowBottomWidth+child.marginTop+child.marginBottom).toInt(),MeasureSpec.AT_MOST)
                child.measure(newWidthMeasureSpec,newHeightMeasureSpec)*/
                //measureChildWithMargins(child, newWidthMeasureSpec, 0, newHeightMeasureSpec, 0)
                //测量child时减去了阴影尺寸，所以要加回来
                cmw=(child.measuredWidth+child.marginLeft+child.marginRight+mShadowLeftWidth+mShadowRightWidth).toInt()
                cmh=(child.measuredHeight+child.marginTop+child.marginBottom+mShadowTopWidth+mShadowBottomWidth).toInt()
                setMeasuredDimension(widthSize,ph+cmh)
            }else if(heightMode==MeasureSpec.EXACTLY){
                //测量child需要减去阴影尺寸
                var newWidthMeasureSpec:Int
                val newHeightMeasureSpec=MeasureSpec.makeMeasureSpec((heightSize-mShadowTopWidth-mShadowBottomWidth).toInt(),MeasureSpec.EXACTLY)
                if(childWidthMode==MeasureSpec.EXACTLY){
                    //此处应为child width
                    newWidthMeasureSpec=MeasureSpec.makeMeasureSpec(childLp.width,MeasureSpec.EXACTLY)
                }else{
                    //TODO 不安全，不能修改child尺寸，因为会多次调用
                    newWidthMeasureSpec=MeasureSpec.makeMeasureSpec((widthSize-mShadowLeftWidth-mShadowRightWidth+childLp.leftMargin+childLp.rightMargin).toInt(),MeasureSpec.AT_MOST)
                }

                if(isChildFillParent()){
                    //TODO 此处child不一定是EXACTLY
                    child.measure(newWidthMeasureSpec,newHeightMeasureSpec)
                }else{
                    measureChildWithMargins(child,widthMeasureSpec,0,heightMeasureSpec,0)
                }

                //测量child时减去了阴影尺寸，所以要加回来
                cmw=(child.measuredWidth+child.marginLeft+child.marginRight+mShadowLeftWidth+mShadowRightWidth).toInt()
                setMeasuredDimension(cmw,heightSize)
            }else{

                //测量child需要减去阴影尺寸

                var newWidthMeasureSpec:Int
                var newHeightMeasureSpec:Int

                /*if(childWidthMode==MeasureSpec.EXACTLY){
                    //此处应为child width
                    newWidthMeasureSpec=MeasureSpec.makeMeasureSpec(childLp.width,MeasureSpec.EXACTLY)
                    newHeightMeasureSpec=MeasureSpec.makeMeasureSpec((heightSize-mShadowTopWidth-mShadowBottomWidth+child.marginTop+child.marginBottom).toInt(),heightMode)
                }else{
                    newWidthMeasureSpec=MeasureSpec.makeMeasureSpec((widthSize-mShadowLeftWidth-mShadowRightWidth+childLp.leftMargin+childLp.rightMargin).toInt(),MeasureSpec.AT_MOST)
                    newHeightMeasureSpec=MeasureSpec.makeMeasureSpec((heightSize-mShadowTopWidth-mShadowBottomWidth+child.marginTop+child.marginBottom).toInt(),heightMode)
                }

                if(childHeightMode==MeasureSpec.EXACTLY){
                    //此处应为child width
                    newHeightMeasureSpec=MeasureSpec.makeMeasureSpec(childLp.height,MeasureSpec.EXACTLY)
                    newWidthMeasureSpec=MeasureSpec.makeMeasureSpec((widthSize-mShadowLeftWidth-mShadowRightWidth+child.marginLeft+child.marginRight).toInt(),widthMode)
                }else{
                    newHeightMeasureSpec=MeasureSpec.makeMeasureSpec((heightSize-mShadowTopWidth-mShadowBottomWidth+childLp.topMargin+childLp.bottomMargin).toInt(),MeasureSpec.AT_MOST)
                    newWidthMeasureSpec=MeasureSpec.makeMeasureSpec((widthSize-mShadowLeftWidth-mShadowRightWidth+child.marginLeft+child.marginRight).toInt(),widthMode)
                }*/

                if(childWidthMode==MeasureSpec.EXACTLY && childHeightMode==MeasureSpec.EXACTLY){
                    newWidthMeasureSpec=MeasureSpec.makeMeasureSpec(childLp.width,MeasureSpec.EXACTLY)
                    newHeightMeasureSpec=MeasureSpec.makeMeasureSpec(childLp.height,MeasureSpec.EXACTLY)
                }else if(childWidthMode==MeasureSpec.EXACTLY){
                    newWidthMeasureSpec=MeasureSpec.makeMeasureSpec(childLp.width,MeasureSpec.EXACTLY)
                    newHeightMeasureSpec=MeasureSpec.makeMeasureSpec((heightSize-mShadowTopWidth-mShadowBottomWidth+child.marginTop+child.marginBottom).toInt(),MeasureSpec.AT_MOST)
                }else if(childHeightMode==MeasureSpec.EXACTLY){
                    newWidthMeasureSpec=MeasureSpec.makeMeasureSpec((widthSize-mShadowLeftWidth-mShadowRightWidth+child.marginLeft+child.marginRight).toInt(),MeasureSpec.AT_MOST)
                    newHeightMeasureSpec=MeasureSpec.makeMeasureSpec(childLp.height,MeasureSpec.EXACTLY)
                }else{
                    newWidthMeasureSpec=MeasureSpec.makeMeasureSpec((widthSize-mShadowLeftWidth-mShadowRightWidth+child.marginLeft+child.marginRight).toInt(),MeasureSpec.AT_MOST)
                    newHeightMeasureSpec=MeasureSpec.makeMeasureSpec((heightSize-mShadowTopWidth-mShadowBottomWidth+child.marginTop+child.marginBottom).toInt(),MeasureSpec.AT_MOST)
                }

                if(isChildFillParent()){
                    child.measure(newWidthMeasureSpec,newHeightMeasureSpec)
                }else{
                    measureChildWithMargins(child,widthMeasureSpec,0,heightMeasureSpec,0)
                }

                //测量child时减去了阴影尺寸，所以要加回来
                cmw=(child.measuredWidth+child.marginLeft+child.marginRight+mShadowLeftWidth+mShadowRightWidth).toInt()
                cmh=(child.measuredHeight+child.marginTop+child.marginBottom+mShadowTopWidth+mShadowBottomWidth).toInt()
                setMeasuredDimension(pw+cmw,ph+cmh)
            }


        }

        //at_most时为最大尺寸，要设置成最小尺寸
       /* if(widthMode==MeasureSpec.EXACTLY && heightMode==MeasureSpec.EXACTLY){
            setMeasuredDimension(widthSize,heightSize)
        }else if(widthMode==MeasureSpec.EXACTLY){
            setMeasuredDimension(widthSize,ph+cmh)
        }else if(heightMode==MeasureSpec.EXACTLY){
            setMeasuredDimension(pw+cmw,heightSize)
        }else{
            setMeasuredDimension(pw+cmw,ph+cmh)
        }
*/




    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        //if(visibility!=View.VISIBLE) return

        //if(count==0) return

        if(childCount==1){
            val child=getChildAt(0)

            if((child.visibility != View.GONE)){
                //自己的padding无法控制
                //因为child设置成了parent的宽高，所以要把child的margin转成child的padding
                child.setPadding( child.paddingLeft+child.marginLeft,child.paddingTop+child.marginTop,child.paddingRight+child.marginRight,child.paddingBottom+child.marginBottom)
                if(mIsChildCenter){
                    child.layout((width-child.measuredWidth)/2,
                        (height-child.measuredHeight)/2,
                        (width-child.measuredWidth)/2+child.measuredWidth,
                        (height-child.measuredHeight)/2+child.measuredHeight)
                }else{
                    child.layout(mShadowLeftWidth.toInt()+paddingLeft,
                        mShadowTopWidth.toInt()+paddingTop,
                        measuredWidth-mShadowRightWidth.toInt()-paddingRight,
                        measuredHeight-mShadowBottomWidth.toInt()-paddingBottom)
                }

            }

        }else if(childCount>1){
            throw Throwable("container can only has one child")
        }else{
            //if(measuredWidth<=0 && measuredHeight<=0) return
        }

       /* setBorderPaint()
        setShapePaint()*/
        setShadowPaint()

        initDrawable()

        val rect=RectF(mShadowLeftWidth+paddingLeft,mShadowTopWidth+paddingTop,measuredWidth.toFloat()-mShadowRightWidth-paddingRight,measuredHeight.toFloat()-mShadowBottomWidth-paddingBottom)
        mPath=Path()
        mPath.addRoundRect(rect,mRadii,Path.Direction.CW)

        //这是阴影背景，所以从shadow_width开始画，阴影晕染由画笔实现
        mShadowRectF=RectF(mShadowLeftWidth+mShadowLeftOffset,mShadowTopWidth+mShadowTopOffset,measuredWidth-mShadowRightWidth-mShadowRightOffset,measuredHeight-mShadowBottomWidth-mShadowBottomOffset)
       // mBorderRectF=RectF(mShadowLeftWidth,mShadowTopWidth,measuredWidth-mShadowRightWidth,measuredHeight-mShadowBottomWidth)
        //mShapeRectF=RectF(mShadowLeftWidth,mShadowTopWidth,measuredWidth-mShadowRightWidth,measuredHeight-mShadowBottomWidth)



      /*  val radii=AlgorithmUtil.formatRadiiFloatArray(mRadius)

        val clipRect=RectF(mShadowLeftWidth,mShadowTopWidth,measuredWidth-mShadowRightWidth,measuredHeight-mShadowBottomWidth)
        mClipPath=Path()
        mClipPath.addRoundRect(clipRect,radii,Path.Direction.CW)

        val shadowClipRect=RectF(0f,0f,measuredWidth.toFloat(),measuredHeight.toFloat())
        mShadowClipPath.addRoundRect(shadowClipRect,radii,Path.Direction.CW)

        mBorderPath=Path()
        mShapePath=Path()

        mBorderPath.addRoundRect(mBorderRectF,radii,Path.Direction.CW)
        mShapePath.addRoundRect(mShapeRectF,radii,Path.Direction.CW)


        if(mStartColor!=Color.TRANSPARENT && mEndColor!=Color.TRANSPARENT){
            //线性渐变,lineargradient只支持两个颜色
            when(mGradientModel){
                0->{
                    mLinearGradient= LinearGradient(0f,mShapeRectF.bottom/2,mShapeRectF.right,mShapeRectF.bottom/2,mStartColor,mEndColor ,Shader.TileMode.CLAMP)
                }
                1->{
                    mLinearGradient= LinearGradient(mShapeRectF.right/2,0f,mShapeRectF.right/2,mShapeRectF.bottom,mStartColor,mEndColor ,Shader.TileMode.CLAMP)
                }
                2->{
                    mLinearGradient= LinearGradient(0f,mShapeRectF.bottom,mShapeRectF.right,0f,mStartColor,mEndColor ,Shader.TileMode.CLAMP)
                }
                3->{
                    mLinearGradient= LinearGradient(0f,0f,mShapeRectF.right,mShapeRectF.bottom,mStartColor,mEndColor ,Shader.TileMode.CLAMP)
                }
            }


        }

        mHasRadius=hasRadius()*/


    }

    fun setStartColor(color: Int){
        mStartColor=color
        initDrawable()
    }

    fun setEndColor(color: Int){
        mEndColor=color
        initDrawable()
    }

    fun setContainerBackgroundColor(color: Int){
        mBackgroundColor=color
        initDrawable()
    }




    private fun initDrawable(){

        lateinit var mPressDrawable: Drawable
        lateinit var mBackgroundDrawable: Drawable
        //lateinit var mDisableDrawable: Drawable

        //不能使用background值，因为已经被设为了透明
        /*if(background is ColorDrawable){
            mBackgroundColor=(background as ColorDrawable).color
        }else{
            val child=getChildAt(0)
             if(childCount>0 && child.background is ColorDrawable)
                 mBackgroundColor=(child.background as ColorDrawable).color
             else
                 mBackgroundColor=Color.WHITE
        }*/


        var gradientOrientation=GradientDrawable.Orientation.LEFT_RIGHT
        when(mGradientModel){
            1->gradientOrientation=GradientDrawable.Orientation.LEFT_RIGHT
            2->gradientOrientation=GradientDrawable.Orientation.RIGHT_LEFT
            3->gradientOrientation=GradientDrawable.Orientation.TOP_BOTTOM
            4->gradientOrientation=GradientDrawable.Orientation.BOTTOM_TOP
            5->gradientOrientation=GradientDrawable.Orientation.BL_TR
            6->gradientOrientation=GradientDrawable.Orientation.TR_BL
            7->gradientOrientation=GradientDrawable.Orientation.TL_BR
            8->gradientOrientation=GradientDrawable.Orientation.BR_TL
        }

        if(mStartColor!=Color.TRANSPARENT && mMiddleColor!=Color.TRANSPARENT && mEndColor!=Color.TRANSPARENT){
            mPressDrawable= GradientDrawable()
            mPressDrawable.orientation= gradientOrientation
            mPressDrawable.colors=intArrayOf(mStartColor,mMiddleColor,mEndColor)
            mPressDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT)

            mBackgroundDrawable= GradientDrawable()
            mBackgroundDrawable.orientation= gradientOrientation
            mBackgroundDrawable.colors=intArrayOf(mStartColor,mMiddleColor,mEndColor)
            mBackgroundDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT)
        }else if(mStartColor!=Color.TRANSPARENT && mMiddleColor==Color.TRANSPARENT && mEndColor!=Color.TRANSPARENT){
            mPressDrawable= GradientDrawable()
            mPressDrawable.orientation= gradientOrientation
            mPressDrawable.colors=intArrayOf(mStartColor,mEndColor)
            mPressDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT)

            mBackgroundDrawable= GradientDrawable()
            mBackgroundDrawable.orientation= gradientOrientation
            mBackgroundDrawable.colors=intArrayOf(mStartColor,mEndColor)
            mBackgroundDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT)
        }else{
            mPressDrawable= GradientDrawable()
            mPressDrawable.setColor(mBackgroundColor)

            mBackgroundDrawable= GradientDrawable()
            mBackgroundDrawable.setColor(mBackgroundColor)
        }

        //mDisableDrawable= GradientDrawable()
        //mDisableDrawable.setColor(mDisableColor)

        mRadii=AlgorithmUtil.formatRadiiFloatArray(mRadius)

        mPressDrawable.cornerRadii=mRadii
        mBackgroundDrawable.cornerRadii=mRadii


        if(mBorderWidth>0f && mBorderColor!=Color.TRANSPARENT){
            mPressDrawable.setStroke(mBorderWidth.toInt(),mBorderColor)
            mBackgroundDrawable.setStroke(mBorderWidth.toInt(),mBorderColor)
        }else{
            mPressDrawable.setStroke(0,0)
            mBackgroundDrawable.setStroke(0,0)
        }

        val sld= StateListDrawable()

        sld.addState(intArrayOf(android.R.attr.state_pressed,android.R.attr.state_enabled),mPressDrawable)
        sld.addState(intArrayOf(android.R.attr.state_enabled),mBackgroundDrawable)
        //sld.addState(intArrayOf(-android.R.attr.state_enabled),mDisableDrawable)


        if(childCount>0 && isChildFillParent()) {
            val child=getChildAt(0)
            child.background=sld
            background=null
        }
        else{
            background=sld
            if(childCount>0){
                val child=getChildAt(0)
                if(childCount>0 && child.background !=null && child.background !is ColorDrawable && child.background !is StateListDrawable){
                    return
                }
                child.background=null
            }

        }
    }

    private fun isChildFillParent():Boolean{
        return mShowShadow || (mBorderWidth>0f && mBorderColor!=Color.TRANSPARENT)
    }

    private fun hasRadius():Boolean{
        return(mRadius[0]>0f || mRadius[1]>0f || mRadius[2]>0f  || mRadius[3]>0f)
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if(canvas==null) return
        if(visibility!=View.VISIBLE) return

        /*if(mShowShadow){
            if( mShadowLeftWidth>0){
                val leftShadowPaint=Paint()
                leftShadowPaint!!.color=mShadowColor
                leftShadowPaint!!.isAntiAlias=true
                // mShadowPaint!!.setShadowLayer(mShadowBlurRadius,0f,0f,mShadowColor)
                leftShadowPaint!!.maskFilter=BlurMaskFilter(mShadowBlurRadius,BlurMaskFilter.Blur.NORMAL)

                val rect=RectF(mShadowLeftWidth,mShadowTopWidth,width.toFloat()-mShadowRightWidth,height.toFloat()-mShadowBottomWidth)
                canvas.drawRoundRect(rect,mShadowRectRadius,mShadowRectRadius,leftShadowPaint)
            }

        }*/

        //画阴影
        mShadowPaint?.let{
            canvas.drawRoundRect(mShadowRectF,mShadowRectRadius,mShadowRectRadius,it)
        }

        //先画阴影再裁剪，否则会把阴影裁剪掉
        canvas.clipPath(mPath)


        //此种方法很矬，且画边框时有明显锯齿


        /*if(mHasRadius){

            //裁剪圆角
            canvas.clipPath(mClipPath)

            //画背景
            mShapePaint?.let {
                mLinearGradient?.let { lg->
                    it.shader=lg
                }
               canvas.drawPath(mShapePath,it)
            }


            //画边框,必须画在背景上，否则有空隙
            mBorderPaint?.let {
                if(mOnlyBottomBorder){
                    canvas.drawLine(mShadowLeftWidth+paddingLeft,
                        height-mShadowBottomWidth,
                        width-mShadowRightWidth-paddingRight,
                        height-mShadowBottomWidth,
                        it)
                }else{
                    canvas.drawPath(mBorderPath,it)
                }
            }


        }else{
            //画背景
            mShapePaint?.let {
                mLinearGradient?.let { lg->
                    it.shader=lg
                }
                canvas.drawRect(mShapeRectF,it)
            }

            //画边框
            mBorderPaint?.let {
                if(mOnlyBottomBorder){
                    canvas.drawLine(mShadowLeftWidth+paddingLeft,
                        height-mShadowBottomWidth,
                        width-mShadowRightWidth-paddingRight,
                        height-mShadowBottomWidth,
                        it)
                }else{
                    canvas.drawPath(mBorderPath,it)
                }
            }

        }*/

    }

    fun setRadius(radius:Float){
        setRadius(floatArrayOf(radius))
    }

    fun setRadius(radius:FloatArray){
        val r=AlgorithmUtil.formatStyleFloatArray(radius)
        mRadius=r
        initDrawable()
    }

    override fun generateDefaultLayoutParams(): MarginLayoutParams? {
        return MarginLayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT
        )
    }

    override fun generateLayoutParams(attrs: AttributeSet?): MarginLayoutParams? {
        return MarginLayoutParams(context, attrs)
    }

    override fun generateLayoutParams(lp: LayoutParams?): LayoutParams? {
        return MarginLayoutParams(lp!!)
    }

    override fun checkLayoutParams(p: LayoutParams?): Boolean {
        return p is MarginLayoutParams
    }



}
